package site.shihuan.helpdesk.auth.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import site.shihuan.helpdesk.auth.exception.AuthResultCode;
import site.shihuan.helpdesk.auth.model.bo.UpdateUserBO;
import site.shihuan.helpdesk.auth.model.vo.UserVO;
import site.shihuan.helpdesk.auth.service.AuthService;
import site.shihuan.helpdesk.auth.service.TenantService;
import site.shihuan.helpdesk.common.model.Constants;
import site.shihuan.helpdesk.common.model.JwtUser;
import site.shihuan.helpdesk.common.model.RestResult;
import site.shihuan.helpdesk.common.model.RestResultUtils;
import site.shihuan.helpdesk.security.context.UserContext;

import javax.validation.constraints.Email;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户认证中心
 *
 * @author 周世焕
 * @Date 2021/1/24 10:39
 */
@RestController
@RequestMapping("/v1/auth/user")
@Api(value = "AuthController",tags = "用户认证中心")
@Validated
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private TenantService tenantService;

    @PostMapping("login")
    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query")
    })
    public RestResult login(@RequestParam String username, @RequestParam String password) {
        UserVO userVO = authService.login(username,password);
        return RestResultUtils.success(userVO);
    }

    @PostMapping("token/refresh")
    public RestResult refreshToken(@RequestParam String refreshToken) {
        return RestResultUtils.success(authService.refreshToken(refreshToken));
    }

    @PostMapping("exists")
    @ApiOperation("检查用户是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "verifyCode", value = "验证码", required = true, dataType = "String", paramType = "query")
    })
    public RestResult exists(@RequestParam String username) {
        boolean result = authService.checkUserExist(username);
        return result ? RestResultUtils.success() : RestResultUtils.failedWithMsg(AuthResultCode.UNAME_OR_PWD_WRONG);
    }

    @GetMapping("ping")
    public RestResult ping() {
        UserContext.getUser();
        return RestResultUtils.success();
    }

    @GetMapping("info")
    @ApiOperation("获取当前会话用户的信息")
    public RestResult getUserInfo() {
        UserVO userVO = authService.getUserInfo();
        //TODO 已登录应该返回更加全面的用户信息
        return userVO != null ? RestResultUtils.success(userVO) :
                RestResultUtils.failedWithMsg(Constants.AUTH_ERR,"未登录");
    }

    @PutMapping("info")
    @ApiOperation("获取当前会话用户的信息")
    public RestResult updateUserInfo(@RequestBody UpdateUserBO userBO) {
        userBO.setUserId(UserContext.getUserId());
        userBO.setTenantId(UserContext.getTenantId());
        authService.updateUser(userBO);
        //TODO 已登录应该返回更加全面的用户信息
        return RestResultUtils.success();
    }

    @GetMapping("info/{uid}")
    @ApiOperation("根据uid获取用户信息")
    public RestResult getUserInfo(@PathVariable String uid) {
        UserVO userVO = authService.getUserInfo(uid);
        return RestResultUtils.success(userVO);
    }

    @PostMapping("register")
    @ApiOperation("注册")
    public RestResult register(@Email @RequestParam String email, @RequestParam String password, @RequestParam String mobile){
        return RestResultUtils.success(authService.register(email,password,mobile));
    }

    /**
     * 获取企业加入请求列表
     * @return
     */
    @GetMapping("join/list")
    public RestResult findJoinRequestList(){
        return RestResultUtils.success(tenantService.findMyJoinRequest(UserContext.getUserId()));
    }
}
